<?php
session_start();
require_once "statics.php";
if(!isset($_SESSION['member_id']))
{
    echo "<script type='text/javascript'>top.window.location='index.php';</script>";
    exit();
}
if(isset($_SESSION['lang']))
    require_once "./language_files/submissionServer_" . $_SESSION['lang'] . ".php";
else
    require_once "./language_files/submissionServer_en.php";
include "functions.php";
$conf_id=mysql_real_escape_string($_POST['conference_id']);
$bRet=bAuthorAll($_SESSION['member_id'],$conf_id);
if(is_string($bRet))
{
    echo"<script type='text/javascript'>top.window.location='main.php?page=submission2.php&conference_id={$conf_id}&failure=" . $bRet . $lang['error_zero'] . __LINE__ . "';</script>";
    exit();
}
if(!$bRet)
{
    echo"<script type='text/javascript'>top.window.location='main.php?page=submission2.php&conference_id={$conf_id}&failure=" . $lang['not_author'] . __LINE__ . "';</script>";
    exit();
}
if($_FILES['paper']['error'] != 0)
{
    echo"<script type='text/javascript'>top.window.location='main.php?page=submission2.php&conference_id={$conf_id}&failure=" . $lang['transfer_error'] . __LINE__ . "';</script>";
    exit();
}
if($_FILES['paper']['size'] > 20000000)
{
    echo"<script type='text/javascript'>top.window.location='main.php?page=submission2.php&conference_id={$conf_id}&failure=" . $lang['file_size_error'] . __LINE__ . "';</script>";
    exit();
}
$type=mysql_real_escape_string($_POST['type']);
$sCategoryName=sCategoryName($type);
if($sCategoryName == "Short Paper" || $sCategoryName == "Long Paper")
{
    if($_FILES['paper']['type'] != "application/pdf")
    {
        echo"<script type='text/javascript'>top.window.location='main.php?page=submission2.php&conference_id={$conf_id}&failure=" . $lang['pdf_error'] . __LINE__ . "';</script>";
        exit();
    }
}
else if($sCategoryName == "Poster")
{
    if($_FILES['paper']['type'] != "image/bmp")
    {
        echo"<script type='text/javascript'>top.window.location='main.php?page=submission2.php&conference_id={$conf_id}&failure=" . $lang['bmp_error'] . __LINE__ . "';</script>";
        exit();
    }
}
else if($sCategoryName == "Image")
{
    if($_FILES['paper']['type'] != "image/jpeg")
    {
        echo"<script type='text/javascript'>top.window.location='main.php?page=submission2.php&conference_id={$conf_id}&failure=" . $lang['jpg_error'] . __LINE__ . "';</script>";
        exit();
    }
}
else if($sCategoryName == "Video")
{
    if($_FILES['paper']['type'] != "video/x-flv")
    {
        echo"<script type='text/javascript'>top.window.location='main.php?page=submission2.php&conference_id={$conf_id}&failure=" . $lang['flv_error'] . __LINE__ . "';</script>";
        exit();
    }
}
$rsDeadline=rsSelect("submission_date","conference_category",array("conference_id=",$_POST['conference_id'],"AND","category_id=",$_POST['type'],"AND","submission_date>","E__CURDATE()"));
if(is_string($rsDeadline))
{
    echo"<script type='text/javascript'>top.window.location='main.php?page=submission2.php&conference_id={$conf_id}&failure=" . $rsDeadline . "ERROR CODE: 0" . __LINE__ . "';</script>";
    exit();
}
if(mysql_num_rows($rsDeadline) == 0)
{
    echo"<script type='text/javascript'>top.window.location='main.php?page=submission2.php&conference_id={$conf_id}&failure=" . "Deadline passed ERROR CODE: 0" . __LINE__ . "';</script>";
    exit();
}
$stamp=time();
$bRet=move_uploaded_file($_FILES['paper']['tmp_name'],"./paper/" . $stamp . $_FILES['paper']['name']);
if(!$bRet)
{
    echo"<script type='text/javascript'>top.window.location='main.php?page=submission2.php&conference_id={$conf_id}&failure=" . $lang['file_move_error'] . __LINE__ . "';</script>";
    exit();
}
$iRet=iInsert("paper",array("paper_title"=>$_FILES['paper']['name'],
    "category_id"=>$type,
    "date_of_submission"=>"E__NOW()",
    "status"=>"To be reviewed",
    "author_id"=>$_SESSION['member_id'],
    "conference_id"=>$conf_id));
if(is_string($iRet))
{
    echo"<script type='text/javascript'>top.window.location='main.php?page=submission2.php&conference_id={$conf_id}&failure=" . $iRet . $lang['error'] . __LINE__ . "';</script>";
    exit();
}
$last_insert=iLastInsert();
if(!is_numeric($last_insert))
{
    echo"<script type='text/javascript'>top.window.location='main.php?page=submission2.php&conference_id={$conf_id}&failure=" . $last_insert . $lang['error'] . __LINE__ . "';</script>";
    exit();
}
$iRet=iInsert("version",array("path"=>"./paper/" . $stamp . $_FILES['paper']['name'],"paper_id"=>$last_insert));
if(is_string($iRet))
{
    echo"<script type='text/javascript'>top.window.location='main.php?page=submission2.php&conference_id={$conf_id}&failure=" . $iRet . $lang['error'] . __LINE__ . "');</script>";
    exit();
}
if($sCategoryName == "Short Paper" || $sCategoryName == "Long Paper")
{
    $output = "";
    if(sOperatingSystem() == "linux")
        $output += shell_exec("pdftohtml_linux/src/pdftohtml -c -noframes " . escapeshellcmd("\"paper/" . $stamp . $_FILES['paper']['name'] . "\""));
    else
        $output += shell_exec("pdftohtml -c -noframes " . escapeshellcmd("\"./paper/" . $stamp . $_FILES['paper']['name'] . "\""));

//    die($output);

    $last_insert=iLastInsert();
    if(!is_numeric($last_insert))
    {
        echo"<script type='text/javascript'>top.window.location='main.php?page=submission2.php&conference_id={$conf_id}&failure=" . $last_insert . $lang['error'] . __LINE__ . "';</script>";
        exit();
    }
    $iRet=iReformat($last_insert);
    if($iRet == -1)
    {
        echo"<script type='text/javascript'>top.window.location='main.php?page=submission2.php&conference_id={$conf_id}&failure=" . $lang['format_error'] . __LINE__ . "';</script>";
        exit();
    }
}
$thisID = mysql_query("SELECT paper_id FROM paper ORDER BY paper_id DESC");
$id = mysql_fetch_assoc($thisID);
$lastID = $id['paper_id'];
echo"<script type='text/javascript'>top.window.location='main.php?page=submission2.php&conference_id={$conf_id}&success=1&id={$lastID}';</script>";
exit();
?>
